clear all
set more off
program drop _all
macro drop _all

//install necessary programs
ssc install estout
ssc install winsor2
ssc install unique
ssc install coefplot
cap ado uninstall ftools
net install ftools, from("https://raw.githubusercontent.com/sergiocorreia/ftools/master/src/")
* Install reghdfe
cap ado uninstall reghdfe
net install reghdfe, from("https://raw.githubusercontent.com/sergiocorreia/reghdfe/master/src/")
* Install ivreg2, the core package
cap ado uninstall ivreg2
ssc install ivreg2
* Finally, install this package
cap ado uninstall ivreghdfe
net install ivreghdfe, from(https://raw.githubusercontent.com/sergiocorreia/ivreghdfe/master/src/)

capture program drop sinh
program sinh
	gen l`1'=ln(`1' + sqrt(`1'^2 + 1))
end

set seed 123
set sortseed 123
/////////// SET PARAMETERS //////////////////

// enter path of directory here
local home "/Users/gauri/Dropbox/patent_persistence/restat_replication_package/" //UPDATE THIS PATH WITH YOUR LOCAL DIRECTORY
display "`home'"
cd "`home'"
pwd

//GLOBALS
global rawdata "`home'rawdata/"
global filedata "`home'filedata/"
global run_do "`home'scripts/"
global results "`home'results/"


//DATA CONSTRUCTION

//0. DATA PROCESSING OUTSIDE OF STATA
//a. gen name gender info-- uses SQL-- we used BigQuery online
//gender_name_build.do
//b. standardize assignee names
//assignee_disambiguation.do


//1. assignee and application data
//a. merge gender and inventor data + fix country info for US states with missing country
do "${run_do}gen_gender_inventor" 
//b. generate disambiguated inventor data for merge (merge only happens later)
do "${run_do}gen_disambig_inventor" 
//c. standardize assignee data
do "${run_do}standardize_assignee" 
//d. merge application and assignee data
do "${run_do}gen_app_assignee" 
//e. merge application-assignee and inventor-gender data
do "${run_do}merge_application_inventor"
//f. merge application-assignee-inventor-gender data with correspondence data
do "${run_do}merge_appinventor_correspondence" 


//2. data cleaning + analysis
//a. data cleaning and merging
//format transaction data
do "${run_do}gen_transaction"
//merge transaction and constructed application data
do "${run_do}merge_transaction_application"
//merge inventor-level (disambig) data
do "${run_do}merge_app_disambig"
//create sample for regs
do "${run_do}gen_sample"


////////////MAIN TEXT TABLES////////////

//Table 1: Relationship between Gender and Patent Application Outcomes (OLS)
do "${run_do}OLS_motivation"
//Table 2: Effect of Initial Rejection on Patent Application Outcomes 
do "${run_do}main_regs_qual"

////////////MAIN TEXT FIGURES////////////
//Figure 1: Probability of Initial Rejection by Examiner Harshness
do "${run_do}harshness_resid_fig"
//Figure 2: Impact of Access to Resources and Environmental Factors on Differential Gender Responses to Initial Rejection and Patent Issuance
do "${run_do}mechanism_figure"
